import time
import random

from sqlalchemy import func, event, JSON
from sqlalchemy_serializer import SerializerMixin
from project_name.ext.database import db


class accounts(db.Model, SerializerMixin):
    id = db.Column(db.String(32), primary_key=True)
    username = db.Column(db.String(32), unique=True)
    real_name = db.Column(db.String(32))
    password = db.Column(db.String(32))
    id_type = db.Column(db.Integer())  # 1: 管理员, 2: 用户
    created_at = db.Column(db.DateTime(timezone=True), server_default=func.now())  # 使用数据库服务器的当前时间
    updated_at = db.Column(db.DateTime)  # 记录最后更新时间
    parent_account_id = db.Column(db.String(32))


# 自动生成ID（时间戳 + 4位随机数）
@event.listens_for(accounts, 'before_insert')
def generate_id(mapper, connection, target):
    if not target.id:
        timestamp = int(time.time() * 1000)  # 毫秒级时间戳
        target.id = f"{timestamp}{random.randint(0, 9999):04d}"


@event.listens_for(accounts, 'before_update')
def update_timestamp(mapper, connection, target):
    target.updated_at = func.now()



class image_features(db.Model, SerializerMixin):
    id = db.Column(db.String(32), primary_key=True)
    doctor_name = db.Column(db.String(32), index=True)
    patient_name = db.Column(db.String(32), index=True)
    image_name = db.Column(db.String(32), index=True)
    image_path = db.Column(db.String(256))
    features = db.Column(JSON)
    created_at = db.Column(db.DateTime(timezone=True), server_default=func.now())  # 使用数据库服务器的当前时间
    updated_at = db.Column(db.DateTime)  # 记录最后更新时间


# 自动生成ID（时间戳 + 4位随机数）
@event.listens_for(image_features, 'before_insert')
def generate_id(mapper, connection, target):
    if not target.id:
        timestamp = int(time.time() * 1000)  # 毫秒级时间戳
        target.id = f"{timestamp}{random.randint(0, 9999):04d}"


@event.listens_for(image_features, 'before_update')
def update_timestamp(mapper, connection, target):
    target.updated_at = func.now()

class categories(db.Model, SerializerMixin):
    id = db.Column(db.String(32), primary_key=True)
    category_name = db.Column(db.String(32), unique=True)
    created_at = db.Column(db.DateTime(timezone=True), server_default=func.now())  # 使用数据库服务器的当前时间
    updated_at = db.Column(db.DateTime)  # 记录最后更新时间


@event.listens_for(categories, 'before_insert')
def generate_id(mapper, connection, target):
    if not target.id:
        timestamp = int(time.time() * 1000)  # 毫秒级时间戳
        target.id = f"{timestamp}{random.randint(0, 9999):04d}"


@event.listens_for(categories, 'before_update')
def update_timestamp(mapper, connection, target):
    target.updated_at = func.now()

class features(db.Model, SerializerMixin):
    id = db.Column(db.String(32), primary_key=True)
    categories_id = db.Column(db.String(32), index=True)
    patient_name = db.Column(db.String(32), index=True)
    image_name = db.Column(db.String(32), index=True)
    image_path = db.Column(db.String(256))
    features = db.Column(JSON)
    created_at = db.Column(db.DateTime(timezone=True), server_default=func.now())  # 使用数据库服务器的当前时间
    updated_at = db.Column(db.DateTime)  # 记录最后更新时间